package com.yue92.datalife.response;

import com.yue92.datalife.common.CodeEnum;
import com.yue92.datalife.common.ICode;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class ResponseResource<T> {

    private Integer code;

    private String message;

    private T data;

    private ResponseResource(ICode code, String message) {
        this.code = code.getCode();
        this.message = message;
    }

    private ResponseResource(ICode code) {
        this.code = code.getCode();
        this.message = code.getMessage();
    }

    public static ResponseResource<?> success() {
        return new ResponseResource<>(CodeEnum.SUCCESS);
    }

    public static <S> ResponseResource<S> success(S data) {
        ResponseResource<S> responseResource = new ResponseResource<>(CodeEnum.SUCCESS);
        responseResource.setData(data);

        return responseResource;
    }

    public static <S> ResponseResource<S> failure(String message, S data) {
        ResponseResource<S> responseResource = new ResponseResource<>(CodeEnum.FAILURE, message);
        responseResource.setData(data);

        return responseResource;
    }

    public static <S> ResponseResource<S> failure(ICode code, String message, S data) {
        ResponseResource<S> responseResource = new ResponseResource<>(code, message);
        responseResource.setData(data);

        return responseResource;
    }

    public static ResponseResource<?> failure(String message) {
        return new ResponseResource<>(CodeEnum.FAILURE, message);
    }
}
